<?php
/**
 * @package model.dao
 * @author Sanu S <sanu@synergyquad.com>
 * @author Modified By Sanu S
 * @copyright Copyright &copy; 2011, synergyquad
 * @since 1.0 - May-18-2011
 * @version 1.1
 */


require_once 'vo/CompanyVO.php';



class CompanyDAOImpl
{
	
	/**
	 * @access public
	 * @staticvar Instance of MemberDAOImpl
	 */
	public static $_instance	= null;
	
	/**
	 * @access public
	 */
	var $_mdb2				= null;
	
	/**
	 * <p>Get an instance of MemberDAOImpl</p>
	 * 
	 * @access public
	 * @return MemberDAOImpl Instance of MemberDAOImpl
	 */
	public static function getInstance() 
	{
		if(self::$_instance == null) 
		{
			self::$_instance = new CompanyDAOImpl();
		}
		
		return self::$_instance;
	}
		
	/**
	 * <p>To get the user start up data by the given user id</p>
	 * 
	 * @access public
	 * @global Array Configuration Array
	 * @param Integer $userId The id of the User	 
	 * @return $userStartUp the UserSettingsVO  and MemberRegVO
	 */
	public function getCompaniesByType($company_type,$start,$count,$searchkey) 
	{
		
          
       $where='';        
        //$query_data = "SELECT administrator.*, company.company_name FROM administrator LEFT JOIN company ON administrator.company=company.id WHERE administrator.email = '".$userName."' AND administrator.password = '".$password."'";
         $query_data = "SELECT * FROM company WHERE company_type = '".$company_type."'";
       //$query_data = "SELECT * FROM administrator WHERE email = '".$userName."' AND password = '".$password."'";        
       if($searchkey)
           $where=" AND(company_name LIKE '%".$searchkey."%' OR street LIKE '%".$searchkey."%' OR city LIKE '%".$searchkey."%' OR state LIKE '%".$searchkey."%')";
       $query_data.=$where."LIMIT ".$start.",".$count;
      $query_count = mysql_fetch_array(mysql_query("SELECT COUNT(*) as count FROM company WHERE company_type = '".$company_type."'".$where));
        $result_data = mysql_query($query_data) or die("Query ERROR");
		$companyVO = new CompanyVO();
                  $arrayCollection = new ArrayCollection();
		$tmp=null;$i=0;
	        while($row=mysql_fetch_object($result_data))
                {
                    $tmp[$i]=$this->createCompanyObject($row);
                    $i++;
                }
                 $arrayCollection->source=$tmp;
                 $arrayCollection->count= $query_count["count"];
                 return  $arrayCollection;
             
       
      

	}
        
        public function getTowerCompany()
       {
          $query_data = "SELECT * FROM company WHERE company_type='Tower company'" ;
        $result_data = mysql_query($query_data) or die("Query ERROR");
        $arrayCollection = new ArrayCollection();
        $tmps = array();
        $i = 0;
       
        while ($row =  mysql_fetch_object($result_data))
        {
            $tmp = new CompanyVO();
            $tmp->company_id = $row->id;
            $tmp->company_name = $row->company_name;
            $tmp->company_type = $row->company_type;
            $tmp->managed_by = $row->managed_by;
            $tmp->street = $row->street;
            $tmp->city = $row->city;
            $tmp->state = $row->state;
            $tmp->country = $row->country;
            $tmp->zip_code = $row->zip_code;
            $tmp->latitude = $row->latitude;
            $tmp->longitude = $row->longitude;
            $tmp->email = $row->email;
            $tmp->phone = $row->phone;
            $tmp->fax = $row->fax;
            $tmp->active = $row->active;
            $tmp->created_by = $row->created_by;
            $tmp->created_date = $row->created_date;
            $tmp->updated_by = $row->updated_by;
            $tmp->updated_date = $row->updated_date;
           
            $tmps[$i] =    $tmp;
            $i++;
        }
        if(count($tmps) >0)
            $arrayCollection->source = $tmps;
        return $arrayCollection;
      }
      
    private function createCompanyObject($row)
    {
    	$tmp = new CompanyVO();
        $tmp->company_id = $row->id;
	$tmp->company_name = $row->company_name;
	$tmp->company_type = $row->company_type;
	$tmp->managed_by = $row->managed_by;
	$tmp->street = $row->street;
	$tmp->city = $row->city;
	$tmp->state = $row->state;
	$tmp->country = $row->country;
	$tmp->zip_code = $row->zip_code;
	$tmp->latitude = $row->latitude;
	$tmp->longitude = $row->longitude;
	$tmp->email = $row->email;
	$tmp->phone = $row->phone;
	$tmp->fax = $row->fax;
	$tmp->active = $row->active;
	$tmp->created_by = $row->created_by;
	$tmp->created_date = $row->created_date;
	$tmp->updated_by = $row->updated_by;
	$tmp->updated_date = $row->updated_date;
        return $tmp;
    }
      
     
} 
?>